package com.teen9g.infoplatformserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.teen9g.PageQueryDTO.FansAndFollowsPageQueryDTO;
import com.teen9g.constant.FansAndFollowsConstant;
import com.teen9g.dto.FansAndFollowsAddDTO;
import com.teen9g.entity.FansAndFollows;
import com.teen9g.infoplatformserver.mapper.FollowMapper;
import com.teen9g.infoplatformserver.mapper.UserMapper;
import com.teen9g.infoplatformserver.service.FollowService;
import com.teen9g.vo.FansVO;
import com.teen9g.vo.FollowsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class FollowServiceImpl implements FollowService {

    @Autowired
    private FollowMapper followMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 添加关注信息
     *
     * @param fansAndFollowsAddDTO 关注信息
     * @return 添加结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String add(FansAndFollowsAddDTO fansAndFollowsAddDTO) {
        //判断是否已经关注
        int temp = followMapper.selectByFansIdAndFollowsId
                (fansAndFollowsAddDTO.getFansId(), fansAndFollowsAddDTO.getFollowsId());
        if (temp != 0) {
            return FansAndFollowsConstant.FOLLOW_EXIST;
        }
        //判断是否是自己关注自己
        if (fansAndFollowsAddDTO.getFansId().equals(fansAndFollowsAddDTO.getFollowsId())) {
            return FansAndFollowsConstant.CANNOT_FOLLOW_SELF;
        }
        //都不是，则添加关注信息
        FansAndFollows fansAndFollows = new FansAndFollows();
        BeanUtils.copyProperties(fansAndFollowsAddDTO, fansAndFollows);
        fansAndFollows.setTrackTime(LocalDateTime.now());

        //被关注者粉丝数+1，关注者关注数+1
        userMapper.increaseFans(fansAndFollowsAddDTO.getFollowsId());
        userMapper.increaseFollows(fansAndFollowsAddDTO.getFansId());

        //返回添加结果
        return followMapper.insert(fansAndFollows) > 0 ?
                FansAndFollowsConstant.FOLLOW_SUCCESS : FansAndFollowsConstant.FOLLOW_FAIL;
    }

    /**
     * 取关
     *
     * @param fansId    粉丝id
     * @param followsId 被关注者id
     * @return 删除结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Integer fansId, Integer followsId) {
        //判断是否存在关注关系
        int fansAndFollows = followMapper.selectByFansIdAndFollowsId(fansId, followsId);
        if (fansAndFollows == 0) {
            return FansAndFollowsConstant.FOLLOW_NOT_EXIST;
        }
        //判断是否是自己取关自己
        if (fansId.equals(followsId)) {
            return FansAndFollowsConstant.CANNOT_UNFOLLOW_SELF;
        }
        //都不是，则删除关注信息
        int delete = followMapper.deleteByFansIdAndFollowsId(fansId, followsId);

        //返回值
        return delete > 0 ? FansAndFollowsConstant.UNFOLLOW_SUCCESS : FansAndFollowsConstant.UNFOLLOW_FAIL;
    }

    /**
     * 分页查询关注信息
     *
     * @param queryDTO 查询条件
     * @return 分页数据
     */
    @Override
    public PageInfo<FollowsVO> pageFollows(FansAndFollowsPageQueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPageNum(), queryDTO.getPageSize());
        List<FollowsVO> list = followMapper.selectFollowsByPage(queryDTO);
        for (FollowsVO followsVO : list) {
            followsVO.setFollowsSignature(userMapper.getSignatureByUserId(followsVO.getFollowsId()));
            followsVO.setFollowsAvatarUrl(userMapper.getAvatarUrlByUserId(followsVO.getFollowsId()));
        }
        return new PageInfo<>(list);
    }

    /**
     * 分页查询粉丝信息
     *
     * @param queryDTO 查询条件
     * @return 分页数据
     */
    @Override
    public PageInfo<FansVO> pageFans(FansAndFollowsPageQueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPageNum(), queryDTO.getPageSize());
        List<FansVO> list = followMapper.selectFansByPage(queryDTO);
        for (FansVO fansVO : list) {
            fansVO.setFansSignature(userMapper.getSignatureByUserId(fansVO.getFansId()));
            fansVO.setFansAvatarUrl(userMapper.getAvatarUrlByUserId(fansVO.getFansId()));
        }
        return new PageInfo<>(list);
    }

    /**
     * 获取关注状态 注意是返回给前端的！！！
     *
     * @param userId1 用户ID1-当前用户
     * @param userId2 用户ID2
     * @return 关注状态
     */
    @Override
    public String getStatus(Integer userId1, Integer userId2) {
        int Id1_Id2 = followMapper.selectByFansIdAndFollowsId(userId1, userId2);
        int Id2_Id1 = followMapper.selectByFansIdAndFollowsId(userId2, userId1);
        //1.两方都没有关注对方
        if (Id1_Id2 == 0 && Id2_Id1 == 0) {
            return FansAndFollowsConstant.STATUS_NOT_FOLLOWED;
        }
        //2.两方都关注了对方
        else if (Id1_Id2 == 1 && Id2_Id1 == 1) {
            return FansAndFollowsConstant.STATUS_TWO_WAY;
        }
        //3.当前用户关注了对方，对方没有关注当前用户
        else if (Id1_Id2 == 1 && Id2_Id1 == 0) {
            return FansAndFollowsConstant.STATUS_FOLLOWED;
        }
        //4.当前用户没有关注对方，对方关注了当前用户
        else {
            return FansAndFollowsConstant.STATUS_BEFORE_TWO_WAY;
        }
    }
}
